var { User, Role, RoleMapUser } = require("./../models.js");
const Token = require("../modules/Token");
const fs = require('fs')
const path = require('path')
var { SystemParams } = require("./../models.js");

module.exports.createRole = async (req, res) => {

	const { user } = req.query
	const { articleId, userId, title, content } = req.body;
	const article = await Article.findByPk(articleId);
	// console.log(article);
	try {
		await article.createRole({
			userId: user && user.id, title, content
		})
		res.sendResult(null, 0, "评论成功")
	} catch (error) {
		res.sendResult(error, 1, "评论失败")

	}
}
module.exports.deleteRole = async (req, res) => {
	const { commentId } = req.body;
	try {
		await Role.destroy({
			where: {
				id: commentId
			}
		})
		res.sendResult(null, 0, "删除评论成功")
	} catch (error) {
		res.sendResult(error, 1, "删除评论成功失败")
	}
}
module.exports.findRole = async (req, res) => {
	const { articleId, page = { pageNumber: 1, pageSize: 10 } } = req.body;
	try {
		const comments = await Role.findAndCountAll({
			include: [{ model: User, attributes: { exclude: ['password'] } }],

			where: { articleId },
			limit: page.pageSize,
			offset: page.pageSize * (page.pageNumber - 1),
		})
		res.sendResult(comments, 0, "查找成功")
	} catch (error) {
		res.sendResult(error, 1, "查找失败")
	}
}

